package com.relytosoft.mizar.analyze.system.services.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.system.api.entity.AyPlanTemplate;
import com.relytosoft.mizar.analyze.system.api.pojo.dto.AyPlanTemplateDto;
import com.relytosoft.mizar.analyze.system.api.pojo.vo.AyPlanTemplateVo;
import com.relytosoft.mizar.analyze.system.services.service.AyPlanTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/16 15:12
 */
@Api(tags = {"计划模板"})
@RestController
@RequestMapping("/system/plantemplate")
public class AyPlanTemplateController {

    @Resource
    private AyPlanTemplateService ayPlanTemplateService;

    @ApiOperation("模板查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(@RequestParam("condition") String condition,
                                                    @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyPlanTemplateDto ayPlanTemplateDto = JSONObject.parseObject(condition, AyPlanTemplateDto.class);
        AyPlanTemplate ayPlanTemplate = BeanCopyUtils.copy(ayPlanTemplateDto, AyPlanTemplate.class);
        IPage<AyPlanTemplate> page = ayPlanTemplateService.selectAyPlanTemplatePageList(pageQuery, ayPlanTemplate);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("添加模板")
    @PostMapping({"/add"})
    public ResponseResult<Void> add(@RequestBody AyPlanTemplateDto ayPlanTemplateDto) {
        AyPlanTemplate ayPlanTemplate = BeanCopyUtils.copy(ayPlanTemplateDto, AyPlanTemplate.class);
        boolean b = ayPlanTemplateService.addAyPlanTemplate(ayPlanTemplate);
        if (b) {
            return ResponseResult.ok("添加模板成功");
        } else {
            return ResponseResult.fail("添加模板失败");
        }
    }

    @ApiOperation("模板详情")
    @GetMapping({"{templateId}"})
    public ResponseResult<AyPlanTemplateVo> id(@PathVariable Integer templateId) {
        return ResponseResult.ok(ayPlanTemplateService.selectAyPlanTemplateById(templateId));
    }

    @ApiOperation("修改模板信息")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody AyPlanTemplateDto ayPlanTemplateDto) {
        AyPlanTemplate ayPlanTemplate = BeanCopyUtils.copy(ayPlanTemplateDto, AyPlanTemplate.class);
        boolean b = ayPlanTemplateService.updateAyPlanTemplate(ayPlanTemplate);
        if (b) {
            return ResponseResult.ok("修改模板成功");
        } else {
            return ResponseResult.fail("修改模板失败");
        }
    }


    @ApiOperation("删除模板信息")
    @DeleteMapping("del")
    public ResponseResult<Void> remove(@RequestBody List<Integer> templateIds) {
        boolean b = ayPlanTemplateService.updateAyPlanTemplateStatus(templateIds);
        if (b) {
            return ResponseResult.ok("删除模板成功");
        } else {
            return ResponseResult.fail("删除模板失败");
        }
    }
}
